package com.yoomiito.app.ui.my;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.yoomiito.app.R;
import com.yoomiito.app.adapter.my.MyCollectGoodsAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.my.MyCollectGoodsBean;
import com.yoomiito.app.present.my.activity.MyCollectGoodsPresenter;
import com.yoomiito.app.ui.RecyclerViewBaseActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.RouterUtils;

import java.util.ArrayList;
import java.util.List;

public class MyCollectGoodsActivity extends RecyclerViewBaseActivity<MyCollectGoodsPresenter> {
    private MyCollectGoodsAdapter mAdapter;
    private List<MyCollectGoodsBean.DataBean> mList;
    private int mDelectId;

    @Override
    public MyCollectGoodsPresenter newP() {
        return new MyCollectGoodsPresenter(App.mAppConfig);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        initView();
//        getNewData();
    }

    @Override
    protected void getNewData() {
        getP().getCollectGoodsData(currentPage);
    }

    @Override
    public String getEmptyContent() {
        return "未收藏商品";
    }

    private void initView() {
        mTitleView.setCenterText(ResUtil.getString(R.string.my_collect_goods));
        mList = new ArrayList<>();
        mSmartRefreshLayout.setPadding(0, 0, 0, 0);
        mAdapter = new MyCollectGoodsAdapter(this, mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnDeleteClickListener((view, position) -> {
            mDelectId = position;
            getP().deletetCollectGood(mAdapter.getData().get(position).getNum_iid());
        });
        mAdapter.setOnItemClickListener((adapter, v, position) -> {
            MyCollectGoodsBean.DataBean data = mAdapter.getData().get(position);
            RouterUtils.toGoodsDetail(MyCollectGoodsActivity.this, String.valueOf(data.getProduct_id()), AllCode.TaoBao);
        });
    }

    public void setCollectGoodsData(MyCollectGoodsBean data) {
        dismissLoading();
        currentPage = data.getCurrent_page();
        if (data.getCurrent_page() == 1) {
            if (currentPage == data.getLast_page()&&data == null || data.getData() == null || data.getData().size() == 0) {
                showEmptyView();
            } else {
                mAdapter.setNewData(data.getData());
                mSmartRefreshLayout.finishRefresh(true);
                if (currentPage == data.getLast_page()) {
                    mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (currentPage == data.getLast_page()) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
            mAdapter.addData(data.getData());
        }
    }

    public void deleteOk() {
        mAdapter.getData().remove(mDelectId);
//        mAdapter.setNewData(mList);
        mAdapter.notifyItemRemoved(mDelectId);
        mRecyclerView.closeMenu();
    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }
}
